<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import { message } from "@/utils/message";
import { FormInstance } from "element-plus";
import {
  addPlatform,
  copyPlatform,
  LabPlatformVO,
  listPlatforms,
  listParentPlatform,
  setDefaultPlatform,
  updatePlatform
} from "@/api/LabPlatformApi";

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object as () => LabPlatformVO,
    default: () => {
      return {};
    }
  }
});

const ruleFormRef = ref<FormInstance>();
const allPlatforms = ref<Array<LabPlatformVO>>([]);

const formVisible = ref(false);
const formData = ref(props.data);
const emit = defineEmits(["update:visible", "add-item"]);
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async valid => {
    if (valid) {
      const validName = await listParentPlatform({
        showParent: true
      });
      const validNameList = validName.data.map(item => item.name);
      if (validNameList.includes(formData.value.name)) {
        message("该实验平台已存在", { type: "error" });
        formVisible.value = false;
        return;
      } else {
        let ret;

        if (formData.value.expId) {
          ret = await updatePlatform(formData.value);
        } else if (formData.value.copyFrom) {
          ret = await copyPlatform(formData.value);
        } else {
          ret = await addPlatform(formData.value);
        }
        if (ret.errorCode !== 0) {
          message(ret.message, { type: "error" });
        } else {
          if (formData.value.isDefault) {
            await setDefaultPlatform(ret.data.id);
          }
          message("提交成功", { type: "success" });
          resetForm(formEl);
          formVisible.value = false;
          emit("add-item");
        }
      }
    }
  });
};
const getLabPlatformData = async () => {
  const result = await listParentPlatform({ showParent: true });
  allPlatforms.value = result.data;
};

onMounted(() => {
  getLabPlatformData();
});

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const closeDialog = () => {
  formVisible.value = false;
  resetForm(ruleFormRef.value);
};

watch(
  () => formVisible.value,
  val => {
    emit("update:visible", val);
  }
);

watch(
  () => props.visible,
  val => {
    formVisible.value = val;
  }
);

watch(
  () => props.data,
  val => {
    formData.value = val;
  }
);

const rules = {
  name: [{ required: true, message: "请输入名称", trigger: "blur" }]
};
</script>

<template>
  <el-dialog
    v-model="formVisible"
    :title="formData.id ? '管理实验平台' : '添加实验平台'"
    :width="680"
    :before-close="closeDialog"
  >
    <!-- 表单内容 -->
    <el-form
      ref="ruleFormRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="formData.name"
          :style="{ width: '480px' }"
          placeholder="请输入名称"
        />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input
          v-model="formData.desc"
          type="textarea"
          :style="{ width: '480px' }"
          placeholder="请输入描述"
        />
      </el-form-item>
      <el-form-item v-if="!formData.id" label="复制配置" prop="copyFrom">
        <el-select v-model="formData.copyFrom" placeholder="请选择复制实验配置">
          <el-option
            v-for="item in allPlatforms"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="设为默认" prop="isDefault">
        <el-checkbox v-model="formData.isDefault" />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
